<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script type='text/javascript' src='../js/vue.js'></script>

</head>

<body>

    <!-- 
            
     -->

    <!-- 
        key原理，使用对象唯一标识  !不要用索引
        如果不写key，Vue会主动将索引作为key
        DOM的diff算法：
            对比之后不一致时，不能复用==>将新的虚拟DOM生成真实DOM
     -->
    <div id="root">

        <!-- 遍历数组 -->
        <h2>成员列表</h2>
        <button @click.once="add">添加成员DING</button>
        <ul>
            <!-- 在v-for中只能接受两个值，第一个值是对象，第二个是索引 -->
            <li v-for='(person,index) in persons' :key='person.id'>
                {{index}}--{{person.name}}-{{person.age}}
                <input/>
            </li>
        </ul>

       

    </div>
    <script>
        Vue.config.productionTip = false;

        const vm = new Vue({
            el: '#root',
            data: {
                persons: [
                    { id: '001', name: 'SONG', age: 18 },
                    { id: '002', name: 'LIU', age: 16 },
                    { id: '003', name: 'MA', age: 19 },
                ]
            },
            methods:{
                add(){
                    const p = {id:'004',name:'DING',age:'20'}
                    this.persons.unshift(p)
                }
            }
               
        })

    </script>
</body>

</html>